import time

import requests

from xieCheng.items import XiechengItem
import scrapy
from scrapy import Selector, Request


class ExampleSpider(scrapy.Spider):
    name = "game"
    allowed_domains = ["4399.com"]

    def __init__(self):
        self.next_href = None
        self.sel = None


    def start_requests(self, **kwargs):
        yield scrapy.Request(
            url='https://www.4399.com/flash/',
        )

    def parse(self, response, **kwargs):
        self.sel = Selector(response)
        game_item = XiechengItem()
        data = self.sel.css('#skinbody > div.bre.oh > ul > li')
        self.next_href = self.sel.xpath('//*[@id="pagg"]/a/@href')[-1].extract()  # 获取最后一个a标签的href
        for i in data:
            game_item['name'] = i.xpath('./a/b/text()').extract_first()
            game_item["type"] = i.xpath('./em/a/text()').extract_first()
            game_item["time"] = i.xpath('./em/text()').extract_first()
            game_item["page"] = str(self.sel.xpath('//*[@id="pagg"]/a[@class="on"]/text()').extract_first())
            game_item["url"] = str(response.urljoin(i.xpath('./a[1]/@href').extract_first()))
            yield game_item
        if self.next_href is not None:
            next_page = response.urljoin(self.next_href)
            yield scrapy.Request(
                url=next_page,
                callback=self.parse,
            )










